
#pragma once

#include "Win32.Memory.h"
#include "Win32.String.h"

// -----------------------------------------------------------------------------

namespace Kif
{
namespace Win32
{
namespace Text
{
namespace Encoding
{

enum EncodingType
{
   EncodingType_Unknown = 0,
   EncodingType_Base64 = 1
}  ;  // end of EncodingType

enum EncodingError
{
   EncodingError_Unknown = -1,
   EncodingError_Success = 0,
   EncodingError_SourceUnderflowNeedMoreData = 1,
   EncodingError_ChunkContainsHalfByteData = 2,
   EncodingError_DecodedOverflow = 3,
   EncodingError_OutputLengthIsNull = 4
}  ;  // end of EncodingError

class Base64
{
private:
public:

   //static void Encode(
   //   TCHAR *outString, size_t outStringLength,
   //   TCHAR *inString,  size_t inStringLength,
   //   size_t *outLength);

   static EncodingError Decode(
      TCHAR *outString, size_t outMaxLength,
      TCHAR *inString,  size_t inLength,
      size_t *outLength);

}  ;  // end of Base64

class DecodedItem
{
private:
public:

   EncodingType  Encoding;
   EncodingError Status;
   TCHAR *Data;
   size_t DataLength;

   bool Decode(EncodingType encoding,
      TCHAR *sourceEncodedItem, size_t sourceEncodedItemLength);

   void Dispose()
   {
      if (Data != NULL)
         Kif::Win32::Memory::free(Data);

      Encoding = EncodingType_Unknown;
      Status   = EncodingError_Unknown;
      Data     = NULL;
      DataLength = 0;
   }

   DecodedItem()
   {
      Data = NULL;
      Dispose();
   }
  ~DecodedItem()
   {
      Dispose();
   }

   TCHAR *Find(TCHAR *buffer, TCHAR *text, size_t textLength,
      Kif::Win32::ItemKeyValue &keyValue, TCHAR *delimiter);

   inline TCHAR *Find(TCHAR *text, size_t textLength,
      Kif::Win32::ItemKeyValue &keyValue, TCHAR *delimiter)
   {
      return(Find(Data,text,textLength,keyValue,delimiter));
   }

   inline TCHAR *Find(TCHAR *text, size_t textLength,
      Kif::Win32::ItemKeyValue &keyValue, TCHAR delimiter)
   {
      TCHAR d[2];
      d[0] = delimiter;
      d[1] = NULL;
      return(Find(text,textLength,keyValue,d));
   }
}  ;  // end of DecodeItem

}  // end of Kif::Win32::Text::Encoding namespace
}  // end of Kif::Win32::Text namespace
}  // end of Kif::Win32 namespace
}  // end of Kif namespace



